﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace jinghuaTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private class Poker
        {

            public int color { get; set; }
            public int num { get; set; }
        }
        //一副原始的扑克牌
        private List<Poker> pokers = new List<Poker>();
        private List<Poker> pokers2 = new List<Poker>();
        private int pokernum = 52;

        private void testButton_Click(object sender, RoutedEventArgs e)
        {

            pokers2.Clear();
            //生成pokers,一副原始的扑克牌
            for (int i = 0; i < 4; i++)
            {
                for (int j = 2; j <= 14; j++)
                {
                    Poker poker = new Poker();
                    poker.color = i;
                    poker.num = j;
                    pokers.Add(poker);
                }
            }


            //洗牌
            Random random = new Random();
            for (int i = 0; i < 1000000; i++)
            {
                int index1 = random.Next(0, 52);
                int index2 = random.Next(0, 52);
                Poker temp = pokers[index1];
                pokers[index1] = pokers[index2];
                pokers[index2] = temp;
            }

            //克隆一副牌
            foreach (Poker poker in pokers)
            {
                pokers2.Add(poker);
            }
            pokernum = 52;


            //发牌
            List<Poker> pokers3 = coutpokers(pokers2, random);
            List<Poker> pokers4 = coutpokers(pokers2, random);
            List<Poker> pokers5 = coutpokers(pokers2, random);
            List<Poker> pokers6 = coutpokers(pokers2, random);
            List<Poker> pokers8 = coutpokers(pokers2, random);
            List<Poker> pokers9 = coutpokers(pokers2, random);
            List<Poker> pokers10 = coutpokers(pokers2, random);
            List<Poker> pokers11 = coutpokers(pokers2, random);
            List<Poker> pokers12 = coutpokers(pokers2, random);
            List<Poker> pokers13 = coutpokers(pokers2, random);
            List<Poker> pokers14 = coutpokers(pokers2, random);
            List<Poker> pokers15 = coutpokers(pokers2, random);
            List<Poker> pokers16 = coutpokers(pokers2, random);
            List<Poker> pokers17 = coutpokers(pokers2, random);
            List<Poker> pokers18 = coutpokers(pokers2, random);
            List<Poker> pokers19 = coutpokers(pokers2, random);
            List<Poker> pokers20 = coutpokers(pokers2, random);
            //发三张牌

            string allHandlePoker = "";
            allHandlePoker += printPokerType(checkPokersHandle(pokers3)) + "::" + putoutpokers(pokers3);
            allHandlePoker += printPokerType(checkPokersHandle(pokers4)) + "::" + putoutpokers(pokers4);
            allHandlePoker += printPokerType(checkPokersHandle(pokers5)) + "::" + putoutpokers(pokers5);
            allHandlePoker += printPokerType(checkPokersHandle(pokers6)) + "::" + putoutpokers(pokers6);
            allHandlePoker += printPokerType(checkPokersHandle(pokers8)) + "::" + putoutpokers(pokers8);
            allHandlePoker += printPokerType(checkPokersHandle(pokers9)) + "::" + putoutpokers(pokers9);
            allHandlePoker += printPokerType(checkPokersHandle(pokers10)) + "::" + putoutpokers(pokers10);
            allHandlePoker += printPokerType(checkPokersHandle(pokers11)) + "::" + putoutpokers(pokers11);
            allHandlePoker += printPokerType(checkPokersHandle(pokers12)) + "::" + putoutpokers(pokers12);
            allHandlePoker += printPokerType(checkPokersHandle(pokers13)) + "::" + putoutpokers(pokers13);
            allHandlePoker += printPokerType(checkPokersHandle(pokers14)) + "::" + putoutpokers(pokers14);
            allHandlePoker += printPokerType(checkPokersHandle(pokers15)) + "::" + putoutpokers(pokers15);
            allHandlePoker += printPokerType(checkPokersHandle(pokers16)) + "::" + putoutpokers(pokers16);
            allHandlePoker += printPokerType(checkPokersHandle(pokers17)) + "::" + putoutpokers(pokers17);
            allHandlePoker += printPokerType(checkPokersHandle(pokers18)) + "::" + putoutpokers(pokers18);
            allHandlePoker += printPokerType(checkPokersHandle(pokers19)) + "::" + putoutpokers(pokers19);
            allHandlePoker += printPokerType(checkPokersHandle(pokers20)) + "::" + putoutpokers(pokers20);

            mainGrid.Children.Clear();

            handPoker handPoker3= disPlayUserPokers(mainGrid, pokers3,0,0);
            handPoker handPoker4 = disPlayUserPokers(mainGrid, pokers4, 0, 1);
            handPoker handPoker5 = disPlayUserPokers(mainGrid, pokers5, 0, 2);
            handPoker handPoker6 = disPlayUserPokers(mainGrid, pokers6, 0, 3);
            handPoker handPoker8 = disPlayUserPokers(mainGrid, pokers8, 0, 4);
            handPoker handPoker9 = disPlayUserPokers(mainGrid, pokers9, 0, 5);
            handPoker handPoker10 = disPlayUserPokers(mainGrid, pokers10, 0, 6);
            handPoker handPoker11 = disPlayUserPokers(mainGrid, pokers11, 0, 7);
            handPoker handPoker12 = disPlayUserPokers(mainGrid, pokers12, 1, 0);
            handPoker handPoker13 = disPlayUserPokers(mainGrid, pokers13, 1, 1);
            handPoker handPoker14 = disPlayUserPokers(mainGrid, pokers14, 1, 2);
            handPoker handPoker15 = disPlayUserPokers(mainGrid, pokers15, 1, 3);
            handPoker handPoker16 = disPlayUserPokers(mainGrid, pokers16, 1, 4);
            handPoker handPoker17 = disPlayUserPokers(mainGrid, pokers17, 1, 5);
            handPoker handPoker18 = disPlayUserPokers(mainGrid, pokers18, 1, 6);
            handPoker handPoker19 = disPlayUserPokers(mainGrid, pokers19, 1, 7);
            handPoker handPoker20 = disPlayUserPokers(mainGrid, pokers20, 2, 0);

            
            Console.WriteLine(allHandlePoker);
            List<Poker> result = new List<Poker>();
            result = ComparePokerHands2(pokers3, pokers4);
            result = ComparePokerHands2(result, pokers5);
            result = ComparePokerHands2(result, pokers6);
            result = ComparePokerHands2(result, pokers8);
            result = ComparePokerHands2(result, pokers9);
            result = ComparePokerHands2(result, pokers10);
            result = ComparePokerHands2(result, pokers11);
            result = ComparePokerHands2(result, pokers12);
            result = ComparePokerHands2(result, pokers13);
            result = ComparePokerHands2(result, pokers14);
            result = ComparePokerHands2(result, pokers15);
            result = ComparePokerHands2(result, pokers16);
            result = ComparePokerHands2(result, pokers17);
            result = ComparePokerHands2(result, pokers18);
            result = ComparePokerHands2(result, pokers19);
            result = ComparePokerHands2(result, pokers20);


            //比出最大的牌

            handPoker lastResult = disPlayUserPokers(mainGrid, result, 3, 6);
            Console.WriteLine(printPokerType(checkPokersHandle(result)) + "::" + putoutpokers(result));
            Console.WriteLine("\n\n");

        }

        private List<Poker> ComparePokerHands2(List<Poker> pokers3, List<Poker> pokers4)
        {
            int big= ComparePokerHands(pokers3, pokers4);

            switch(big)
            {
                case 1:
                    return pokers3;
                    case -1:
                    return pokers4;
                    default:
                    return pokers3;
            }
        }

        private handPoker disPlayUserPokers(Grid grid,List<Poker> handpoker,int x,int y) {


            //方块的字符♠♣♥♦
            Tuple<string, string,Color> poker1 = checkpokerFlower(handpoker[0]);
            Tuple<string, string, Color> poker2 = checkpokerFlower(handpoker[1]);
            Tuple<string, string, Color> poker3 = checkpokerFlower(handpoker[2]);

            //背景设置为#108ee9
            



            //red
            handPoker handPoker = new handPoker();
            handPoker.poker1.pokNum.Text = poker1.Item2;
            handPoker.poker1.pokIco.Text = poker1.Item1;
            handPoker.poker1.pokIcB.Text = poker1.Item1;
            handPoker.poker1.pokIcC.Text = handpoker[0].num>10 && handpoker[0].num != 14 ? "꧃‍" : poker1.Item1;///
            handPoker.poker1.pokNum.Foreground = new SolidColorBrush(poker1.Item3);
            handPoker.poker1.pokIco.Foreground = new SolidColorBrush(poker1.Item3);
            handPoker.poker1.pokIcB.Foreground = new SolidColorBrush(poker1.Item3);
            handPoker.poker1.pokIcC.Foreground = handpoker[0].num > 10 && handpoker[0].num != 14 ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")) : new SolidColorBrush(poker1.Item3);

            handPoker.poker2.pokNum.Text = poker2.Item2;
            handPoker.poker2.pokIco.Text = poker2.Item1;
            handPoker.poker2.pokIcB.Text = poker2.Item1;
            handPoker.poker2.pokIcC.Text = handpoker[1].num > 10 && handpoker[1].num != 14 ? "꧃‍" : poker2.Item1;///
            handPoker.poker2.pokNum.Foreground = new SolidColorBrush(poker2.Item3);
            handPoker.poker2.pokIco.Foreground = new SolidColorBrush(poker2.Item3);
            handPoker.poker2.pokIcB.Foreground = new SolidColorBrush(poker2.Item3);
            handPoker.poker2.pokIcC.Foreground = handpoker[1].num > 10 && handpoker[1].num != 14 ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")) : new SolidColorBrush(poker2.Item3);

            handPoker.poker3.pokNum.Text = poker3.Item2;
            handPoker.poker3.pokIco.Text = poker3.Item1;
            handPoker.poker3.pokIcB.Text = poker3.Item1;
            handPoker.poker3.pokIcC.Text = handpoker[2].num > 10&& handpoker[2].num != 14 ? "꧃‍" : poker3.Item1;///
            handPoker.poker3.pokNum.Foreground = new SolidColorBrush(poker3.Item3);
            handPoker.poker3.pokIco.Foreground = new SolidColorBrush(poker3.Item3);
            handPoker.poker3.pokIcB.Foreground = new SolidColorBrush(poker3.Item3);
            handPoker.poker3.pokIcC.Foreground = handpoker[2].num > 10 && handpoker[2].num != 14 ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC000")) : new SolidColorBrush(poker3.Item3);





            Grid.SetRow(handPoker, x);
            Grid.SetColumn(handPoker, y);
            Grid.SetRowSpan(handPoker, 1);
            Grid.SetColumnSpan(handPoker, 1);
            grid.Children.Add(handPoker);


            return handPoker;
        }



        private Tuple<string,string,Color> checkpokerFlower(Poker poker)
        {

               Color yanse= Color.FromRgb(255, 0, 0);

                string color = "";
                switch (poker.color)
                {
                    case 0:
                        color = "♠";
                    yanse = Color.FromRgb(0, 0, 0);

                    break;
                    case 1:
                        color = "♥";
                    yanse = Color.FromRgb(255, 0, 0);

                    break;
                    case 2:
                        color = "♣";
                    yanse = Color.FromRgb(0, 0, 0);

                    break;
                    case 3:
                        color = "♦";
                    yanse = Color.FromRgb(255, 0, 0);

                    break;
                }

                string num = "";
                switch (poker.num)
                {


                    case 14:
                        num = "A";
                        break;
                    case 11:
                        num = "J";
                        break;
                    case 12:
                        num = "Q";
                        break;
                    case 13:
                        num = "K";
                        break;
                    default:
                        num = poker.num.ToString();
                        break;
                }


            return new Tuple<string, string,Color>(color,num,yanse);
        }



        //显示三张牌
        private string putoutpokers(List<Poker> pokers)
        {

            string allHandlePoker = "";
            foreach (Poker poker in pokers)
            {
                string color = "";
                switch (poker.color)
                {
                    case 0:
                        color = "黑桃";
                        break;
                    case 1:
                        color = "红桃";
                        break;
                    case 2:
                        color = "梅花";
                        break;
                    case 3:
                        color = "方块";
                        break;
                }

                string num = "";
                switch (poker.num)
                {


                    case 14:
                        num = "A";
                        break;
                    case 11:
                        num = "J";
                        break;
                    case 12:
                        num = "Q";
                        break;
                    case 13:
                        num = "K";
                        break;
                    default:
                        num = poker.num.ToString();
                        break;
                }

                allHandlePoker += ".."+color + num;
            }

            return allHandlePoker + "\n";
        }

        //发牌
        private List<Poker> coutpokers(List<Poker> mainPokers, Random random)
        {

            List<Poker> pokers3 = new List<Poker>();
            for (int i = 0; i < 3; i++)
            {
                int index1 = random.Next(0, pokernum);
                pokers3.Add(mainPokers[index1]);
                mainPokers.RemoveAt(index1);
                pokernum--;
            }

            pokers3.Sort((poker1, poker2) =>
            {
                return poker1.num - poker2.num;
            });

            return pokers3;

        }


        /// <summary>
        /// 检查牌型
        /// </summary>
        /// <param name="pokersIn"></param>
        /// <returns></returns>
        private int checkPokersHandle(List<Poker> pokersIn)
        {

            //任意两张大小相同，第三张不同则为对子
            if (
                (pokersIn[0].num == pokersIn[1].num || pokersIn[1].num == pokersIn[2].num || pokersIn[0].num == pokersIn[2].num)//任意两张大小相同
                &&
                !(pokersIn[0].num == pokersIn[1].num&& pokersIn[1].num == pokersIn[2].num)
                )//三张不同
            {
                return 1;
            }

            //任意大小相差1，且花色不同则为顺子
            if (
                ((pokersIn[1].num - pokersIn[0].num == 1 && pokersIn[2].num - pokersIn[1].num == 1)|| (pokersIn[0].num == 2 && pokersIn[1].num == 3 && pokersIn[2].num == 14))//任意大小相差1
                &&
               !(pokersIn[0].color == pokersIn[1].color && pokersIn[1].color == pokersIn[2].color)
               )//花色不同
            {
                return 2;
            }


            //同花
            if (
                !(pokersIn[1].num - pokersIn[0].num == 1 && pokersIn[2].num - pokersIn[1].num == 1) //不是顺子
                &&
                !(pokersIn[0].num == 2 && pokersIn[1].num == 3 && pokersIn[2].num == 14)//不是特殊顺子
                &&
                (pokersIn[0].color == pokersIn[1].color && pokersIn[1].color == pokersIn[2].color && pokersIn[0].color == pokersIn[2].color))//花色相同
            {
                return 3;
            }
            


            //同花顺
            if (
                ((pokersIn[1].num - pokersIn[0].num == 1 && pokersIn[2].num - pokersIn[1].num == 1 )||(pokersIn[0].num == 2 && pokersIn[1].num == 3 && pokersIn[2].num == 14))//顺子
                &&
                (pokersIn[0].color == pokersIn[1].color && pokersIn[1].color == pokersIn[2].color && pokersIn[0].color == pokersIn[2].color))//花色相同
            {

                return 4;
            }
            

            //三条
            if (pokersIn[0].num == pokersIn[1].num && pokersIn[1].num == pokersIn[2].num && pokersIn[0].num == pokersIn[2].num)//三张相同
            {
                return 5;
            }



            return 0;
        }



        private string printPokerType(int i)
        {


            switch (i)
            {
                case 1:
                    return "对子";
                case 2:
                    return "顺子";
                case 3:
                    return "同花";
                case 4:
                    return "花顺";
                case 5:
                    return "三条";
                default:
                    return "。。";//"散牌";
            }
        }



        /// <summary>
        /// 对比两手牌的大小
        /// </summary>
        /// <param name="hand1"></param>
        /// <param name="hand2"></param>
        /// <returns></returns>
        private int ComparePokerHands(List<Poker> hand1, List<Poker> hand2)
        {
            int type1 = checkPokersHandle(hand1); // 获取手牌1的牌型
            int type2 = checkPokersHandle(hand2); // 获取手牌2的牌型

            // 首先比较牌型
            if (type1 > type2)
            {
                return 1; // 手牌1胜
            }
            else if (type1 < type2)
            {
                return -1; // 手牌2胜
            }
            else
            {
                // 牌型相同，需要比较牌面大小
                if (type1 == 1) // 对子
                {               
                    Tuple<int, int> pair1 =
                        hand1[0].num == hand1[1].num ? new Tuple<int,int>(hand1[0].num, hand1[2].num) :
                        hand1[1].num == hand1[2].num ? new Tuple<int, int>(hand1[1].num, hand1[0].num) :
                        hand1[0].num == hand1[2].num ? new Tuple<int, int>(hand1[2].num, hand1[1].num) :
                        new Tuple<int, int>(-1,-1); // 对子牌的数字
                    Tuple<int, int> pair2 =
                        hand2[0].num == hand2[1].num ? new Tuple<int, int>(hand2[0].num, hand2[2].num) :
                        hand2[1].num == hand2[2].num ? new Tuple<int, int>(hand2[1].num, hand2[0].num) :
                        hand2[0].num == hand2[2].num ? new Tuple<int, int>(hand2[2].num, hand2[1].num) :
                        new Tuple<int, int>(-1, -1); // 对子牌的数字


                    if (pair1.Item1 >pair2.Item1)
                    {
                        return 1; // 手牌1胜
                    }
                    else if (pair1.Item1 < pair2.Item1)
                    {
                        return -1; // 手牌2胜
                    }
                    else
                    {
                        // 对子牌面相同，需要比较单牌
                        if (pair1.Item2>pair2.Item2)
                        {
                            return 1;
                        }else if (pair1.Item2 < pair2.Item2)
                        {
                            return -1;
                        }
                        return 0; // 平局
                    }
                }
                else
                {
                    // 其他牌型比较顺序为：同花顺 > 三条 > 同花 > 顺子
                    if (hand1[2].num > hand2[2].num)
                    {
                        return 1;
                    }
                    else if (hand1[2].num < hand2[2].num)
                    {
                        return -1;
                    }
                    else if (hand1[1].num > hand2[1].num)
                    {
                        return 1;
                    }
                    else if (hand1[0].num > hand2[0].num)
                    {
                        return 1;
                    }


                    return 0; // 平局
                }
            }
        }

    }
}
